package model;

/**
 * Created by Esferat on 02.08.2014.
 */
import org.openqa.selenium.*;
import org.openqa.selenium.interactions.Actions;
import org.openqa.selenium.support.ui.ExpectedCondition;
import org.openqa.selenium.support.ui.Select;
import org.openqa.selenium.support.ui.WebDriverWait;

import static org.openqa.selenium.support.ui.ExpectedConditions.*;

import java.util.List;

/**
 * Base class for all page-helper objects.
 * Holds functionality common for all pages, and mostly hides WebDriver interaction.
 */

public class HelperBase {

    public static final int ELEMENT_TIMEOUT = 5;
    public static final int PAGE_TIMEOUT = 10;
    protected final TestManager manager;
    protected final WebDriver driver;

    /*
     * Takes a reference to static TestManager instance as input.
     */
    public HelperBase(TestManager manager) {
        this.manager = manager;
        driver = manager.getDriver();
    }

    public String getBaseUrl() {
        return manager.getBaseUrl();
    }

    public WebElement findElement(By locator) {
        return driver.findElement(locator);
    }

    public List<WebElement> findElements(By locator) {
        return driver.findElements(locator);
    }

    public void type(By locator, String text) {
        driver.findElement((locator)).clear();
        driver.findElement((locator)).sendKeys(text);
    }

    public void select(By locator, String value) {
        new Select(driver.findElement(locator)).selectByVisibleText(value);
    }

    public void click(By locator) {
        findElement((locator)).click();
    }

    public String getText(By locator) {
        return findElement((locator)).getText();
    }

    public boolean textIsPresent(By locator, String text) {
        if(getText(locator).equals(text))
        {
            return true;
        } else {
            return false;
        }
    }

    public String getValue(By locator) {
        return findElement((locator)).getAttribute("value");
    }

    public void openUrl(String string) {
        driver.get(string);
    }

    public boolean isElementPresent(By by) {
        try {
            driver.findElement(by);
            return true;
        } catch (NoSuchElementException e) {
            return false;
        }
    }

    public void waitForElementVisible(By locator) {
        waitFor(visibilityOfElementLocated(locator));
    }

    public void waitForElementInDOM(By locator) {
        waitFor(presenceOfElementLocated(locator));
    }

    public void waitForElementInvisible(By locator) {
        waitFor(invisibilityOfElementLocated(locator));
    }

    public void waitForElementRefreshed(WebElement webElement) {
        waitFor(refreshed(visibilityOf(webElement)));
    }

    protected void moveTheMouse(WebElement locator) {
        new Actions(driver).moveToElement(locator).perform();
    }

    protected void waitFor(ExpectedCondition<?> condition) {
        new WebDriverWait(driver, ELEMENT_TIMEOUT).until(condition);
    }

    protected boolean switchToWindow(int numWindow) {
        if((numWindow + 1) <= driver.getWindowHandles().toArray().length){
            driver.switchTo().window(driver.getWindowHandles().toArray()[numWindow].toString());
            return true;
        }
        else{
            return false;
        }
    }

    protected void closeCurrentWindow() {
        driver.close();
    }

}
